package com.example.PacMan;

import java.io.*;
import java.util.Vector;


//to record info
public class Recorder {
    private static int allPointNum = 0;
    private static int allGhostNum = 3;
    private static BufferedReader br = null;
    private static BufferedWriter bw = null;
    private static String recordFile01 = "/Users/liweidong/Desktop/myRecord1.txt";
    private static String recordFile02 = "/Users/liweidong/Desktop/myRecord2.txt";
    //define Vector, point to point of MyPanel
    private static Vector<Ghost> ghosts = null;
    private static Vector<Point> points = null;

    //define Node Vector
    private static Vector<Node01> node01s = new Vector<>();
    private static Vector<Node02> node02s = new Vector<>();

    public static void setGhosts(Vector<Ghost> ghosts) {
        Recorder.ghosts = ghosts;
    }

    public static void setPoints(Vector<Point> points) {
        Recorder.points = points;
    }

    //to read file, and recover info
    public static Vector<Node01> getNode01AllGhost(){
        try {
            br = new BufferedReader(new FileReader(recordFile01));
            String line = br.readLine();
            System.out.println("the value read from file is :" + line);
            allGhostNum = Integer.parseInt(line);
            //String line = "";
            while ((line = br.readLine()) != null){
                String[] xyd = line.split(" ");
                Node01 node01 = new Node01(Integer.parseInt(xyd[0]),Integer.parseInt(xyd[1]),Integer.parseInt(xyd[2]));
                node01s.add(node01);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if (br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return node01s;
    }
    public static Vector<Node02> getNode02AllPoint(){
        try {
            br = new BufferedReader(new FileReader(recordFile02));
            allPointNum = Integer.parseInt(br.readLine());
            String line = "";
            while ((line = br.readLine()) != null){
                String[] xy = line.split(" ");
                Node02 node02 = new Node02(Integer.parseInt(xy[0]),Integer.parseInt(xy[1]));
                node02s.add(node02);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if (br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return node02s;
    }

    public static void keepRecord01(){
        try {
            bw = new BufferedWriter(new FileWriter(recordFile01));
            bw.write(allGhostNum + "\r\n");
            //OOP,define property
                for (int i = 0; i < ghosts.size(); i++) {
                    //get every points
                    Ghost ghost = ghosts.get(i);
                    if (ghost.isLive) {
                        //save the point of data
                        String record = ghost.getX() + " " + ghost.getY() + " " + ghost.getDirect();
                        //write to file
                        System.out.println("Writing to file: " + bw);
                        bw.write(record + "\r\n");
                    }
                }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if (bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void keepRecord02(){
        try {
            bw = new BufferedWriter(new FileWriter(recordFile02));
            bw.write(allPointNum + "\r\n");
            for (int i = 0; i < points.size(); i++) {
                Point point = points.get(i);
                if (point.isLive){
                    String record = point.getX() + " " + point.getY();
                    bw.write(record + "\r\n");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if (bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static int getAllPointNum() {
        return allPointNum;
    }

    public static void setAllPointNum(int allPointNum) {
        Recorder.allPointNum = allPointNum;
    }

    public static void addAllPointNum(){
        Recorder.allPointNum++;
    }
}
